<template>
  <div class='container'>
    <div class="app-container">
      <el-card shadow="never">
        <!-- 表单 -->
         <el-form :model="form">
          <el-form-item label="学科名称" prop="subjectName">
            <el-input
            size="small"
              style="width: 200px;margin-right:10px"
              v-model="form.subjectName"
              class="filter-item"
            ></el-input>
            <el-button
              class="filter-item"
              size="small"
              @click="clear"
            >清除</el-button>
            <el-button class="filter-item" size="small" type="primary" @click="search">搜索</el-button>
            <el-button
              class="filter-item fr"
              size="small"
              style="margin-left: 10px;"
              type="success"
              icon="el-icon-edit"
              @click="showDialog=true"
            >新增学科</el-button>
          </el-form-item>
        </el-form>
        <!-- 弹框 -->
        <el-alert
          type="info"
          class="alert"
          show-icon
          :closable="false"
          :title="`数据一共${total}条`"
        ></el-alert>
        <!-- 表格 -->
        <el-table
        :data="subjects"
        >
          <el-table-column label="序号" type="index"></el-table-column>
          <el-table-column label="学科名称" prop="subjectName" ></el-table-column>
          <el-table-column label="创建者" prop="username" ></el-table-column>
          <el-table-column label="创建日期">
            <template #default="{row}">
              {{row.addDate | parseTimeByString}}
            </template>
          </el-table-column>
          <el-table-column label="前台是否显示" >
            <template #default="{row}">
              {{row.isFrontDisplay === 1 ? '是' : '否'}}
            </template>
          </el-table-column>
          <el-table-column label="二级目录" prop="twoLevelDirectory"></el-table-column>
          <el-table-column label="标签" prop="tags" ></el-table-column>
          <el-table-column label="题目数量" prop="totals" ></el-table-column>
          <el-table-column label="操作" width="300px">
            <template #default="{row}">
              <el-button type="text" size="small" @click="$router.push({ path: 'directorys',query:{id:row.id,name:row.subjectName} })">学科分类</el-button>
              <el-button type="text" size="small" @click="$router.push({ path: 'tags',query:{id:row.id,name:row.subjectName} })">学科标签</el-button>
              <el-button type="text" size="small" @click="showEdit(row)">修改</el-button>
              <el-button type="text" size="small" @click="delSubjects(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
      <el-pagination
      class="pager"
      background
      :total="total"
      :page-sizes="[1,2,5,10]"
      :page-size="pagesize"
      :current-page="page"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      layout="total,sizes, prev, pager, next, jumper"
      >
      </el-pagination>
      <subjects-add :show-dialog.sync="showDialog"  @subjects-add="getSubjects" ref="addRef"></subjects-add>
      </el-card>
    </div>
  </div>
</template>

<script>
// import { parseTimeByString } from '@/filters/index'
import { list as subjectsList, remove, detail } from '@/api/hmmm/subjects.js'
import SubjectsAdd from '../components/subjects-add.vue'
export default {
  components: {
    SubjectsAdd
  },
  // props: ['total', 'paginationPage', 'paginationPagesize'],
  data () {
    return {
      showDialog: false,
      subjects: [],
      form: {
        subjectName: ''
      },
      page: 1,
      pagesize: 10,
      total: 0
    }
  },
  created () {
    this.getSubjects()
  },
  methods: {
    async showEdit (row) {
      // console.log(id)
      // console.log(id)
      const { data } = await detail(row)
      // console.log(data)
      // 回显数据
      this.$refs.addRef.form = data
      // 显示弹层
      this.showDialog = true
    },
    delSubjects (id) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id })
        this.$message.success('删除学科成功')
        if (this.subjects.length === 1 && this.page > 1) {
          this.page--
        }
        this.getSubjects()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async search () {
      // this.pagesize = 1
      // this.form.subjectName = val
      // console.log(this.form.subjectName)
      // this.getSubjects({ subjectName: val })
      const { data } = await subjectsList({ page: 1, subjectName: this.form.subjectName })
      // console.log(data)
      this.total = data.counts
      this.subjects = data.items
    },
    clear () {
      this.form = {
        subjectName: ''
      }
      this.page = 1
      this.pagesize = 10
      this.getSubjects()
    },
    async getSubjects () {
      // this.loading = true
      const { data } = await subjectsList({ page: this.page, pagesize: this.pagesize })
      this.subjects = data.items
      // console.log(data)
      this.total = data.counts
      // this.loading = false
    },
    handleSizeChange (val) {
      this.pagesize = val
      this.page = 1
      this.getSubjects()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.page = val
      this.getSubjects()
    }
  }
}
</script>

<style scoped lang='scss'>
.alert {
  margin: 10px 0px;
}
::v-deep{
  .table_header{
    background-color:#fafafa;
    border-bottom: 2px solid #e8e8e8;
  }
}
.pager{
  margin-top: 15px;
  float: right;
}
</style>
